﻿using System;
using System.Collections.Generic;
using System.Linq;
using Shop.Base.Entity;
using Shop.Base.Entity.Delete;
using Shop.Base.Entity.Insert;
using Shop.Base.Entity.Update;
using Shop.Base.Entity.View;
using Shop.Base.Interface;
using Shop.Entity;
using Shop.Server;
using Shop.Tools;

namespace Shop.Base.Server
{
    public class SystemRoleFunction : BaseServer<SystemRoleFunctionPo>, ISystemRoleFunction
    {
        public List<SystemRoleFunctionPo> GetAll(Guid roleId)
        {
            return db.Queryable<SystemRoleFunctionPo>().Where(x => x.RoleId == roleId).ToList();
        }

        public List<SystemRoleFunctionPo> GetAll(List<Guid> roleIds)
        {
            return db.Queryable<SystemRoleFunctionPo>().Where(x => roleIds.Contains(x.Id)).ToList();
        }

        public bool MultSave(List<string> t, Guid roleId, ComUser user)
        {
            try
            {
                var allRoleMenu = GetAll(roleId);
                db.Ado.BeginTran();
                var list = new List<SystemRoleFunctionPo>();
                if (t.Any())
                {
                    t.ForEach(x =>
                    {
                        var str = x.Contains("=") ? (x.Split('=')[0].IsNullOrEmpty()?x.Split('=')[1]: x.Split('=')[0]) : x;
                        if (str.Contains("*"))
                        {
                            var arr = str.Split('*').ToList();
                            arr.ForEach(y =>
                            {
                                if (y.Contains("|"))
                                {
                                    var menuButton = y.Split('|');
                                    var menuId = menuButton[0].ToGuid();
                                    var buttonId = menuButton[1].ToGuid();
                                    if (!list.Any(c => c.RoleId == roleId && c.MenuId == menuId && c.ButtonId == buttonId))
                                    {
                                        list.Add(new SystemRoleFunctionPo
                                        {
                                            RoleId = roleId,
                                            MenuId = menuId,
                                            ButtonId = buttonId
                                        });
                                    }
                                }
                                else
                                {
                                    if (!list.Any(c => c.RoleId == roleId && c.MenuId == y.ToGuid() && c.ButtonId == Guid.Empty))
                                    {
                                        list.Add(new SystemRoleFunctionPo
                                        {
                                            RoleId = roleId,
                                            MenuId = y.ToGuid(),
                                        });
                                    }
                                }
                            });
                        }
                        else
                        {
                            if (!list.Any(c => c.RoleId == roleId && c.MenuId == str.ToGuid() && c.ButtonId == Guid.Empty))
                            {
                                list.Add(new SystemRoleFunctionPo
                                {
                                    RoleId = roleId,
                                    MenuId = str.ToGuid(),
                                });
                            }
                        }
                    });

                    list.ForEach(x =>
                    {
                        if (!allRoleMenu.Exists(c => c.ButtonId == x.ButtonId && c.MenuId == x.MenuId && c.RoleId == x.RoleId))
                        {
                            db.Insertable(new AddSystemRoleFunction
                            {
                                MenuId = x.MenuId,
                                CreateId = user.Id,
                                CreateName = user.UserName,
                                RoleId = x.RoleId,
                                ButtonId = x.ButtonId
                            }).ExecuteCommand();
                        }
                        else
                        {
                            var mo = allRoleMenu.FirstOrDefault(c =>c.ButtonId == x.ButtonId && c.MenuId == x.MenuId && c.RoleId == x.RoleId);
                            db.Updateable(new UpdateSystemRoleFunction
                            {
                                Id = mo.Id,
                                MenuId = mo.MenuId,
                                ButtonId = mo.ButtonId,
                                RoleId = mo.RoleId,
                                UpdateId = user.Id,
                                UpdateAt = DateTime.Now,
                                UpdateName = user.UserName,
                                IsDel = false
                            }).ExecuteCommand();
                        }
                    });
                }
                allRoleMenu.ForEach(x =>
                {
                    if (!list.Exists(c => c.ButtonId == x.ButtonId && c.MenuId == x.MenuId && c.RoleId == x.RoleId))
                    {
                        x.IsDel = true;
                        x.UpdateAt = DateTime.Now;
                        x.UpdateId = user.Id;
                        x.UpdateName = user.UserName;
                        db.Updateable(x).ExecuteCommand();
                    }
                });
                db.Ado.CommitTran();
                return true;
            }
            catch (Exception e)
            {
                db.Ado.RollbackTran();
                throw e;
            }
        }

        public List<UserRoleFunction> GetUserRoleFunction(List<Guid> roleIds)
        {
            return db.Queryable<UserRoleFunction>().Where(x => roleIds.Contains(x.RoleId)).OrderBy("IndexNum").ToList();
        }
    }
}